package org.ssi.web.util;

import java.sql.Clob;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.StringTokenizer;

/**
 * @author ��ظ�
 * @version 1.0
 * history  
 * editor             date             memo
 * ===========================================================
 * tongzw             2006.4.16        ��ʼ�����
 * jiaorg             2006.4.17        ��ӹ��ܺ���toStrmoney
 */

public class StringHandle {
	public StringHandle() {
	}

	/**
	 * ת�������ַ� ���� < > '��
	 * @param str a String
	 * @return String
	 */
	public static String getConvertString(String str) {
		if (str == null) {
			return "";
		}
		String strs = "";
		char[] t = str.toCharArray();
		for (int i = 0; i < t.length; i++) {
			switch (t[i]) {
			case '>':
				strs += "&gt;";
				break;
			case '<':
				strs += "&lt;";
				break;
			case 39:
				strs += "&#39;";
				break;
			case 32:
				strs += "&nbsp;";
				break;
			case 10:
				strs += "<br>";
				break;
			default:
				strs += t[i];
			}
		}
		return strs;
	}

	/**
	 * ת�������ַ� ���� < > '��
	 * @param str a String
	 * @return String
	 */
	public String getHtmlString(String str) {
		if (str == null) {
			return "";
		}
		String strs = "";
		char[] t = str.toCharArray();
		for (int i = 0; i < t.length; i++) {
			switch (t[i]) {
			case '>':
				strs += "&gt;";
				break;
			case '<':
				strs += "&lt;";
				break;
			default:
				strs += t[i];
			}
		}
		return strs;
	}

	/**
	 * ��ԭ�����ַ� ���� & gt;  & lt;  ��
	 * @param str a String
	 * @return String
	 */
	public String getRevertString(String str) {
		if (str == null) {
			//	com.bjtech.util.StringHandle.printLog("this?");
			return "";
		}
		str = replace(str, "&nbsp;", " ");
		str = replace(str, "<br>", "\n");
		str = replace(str, "&gt;", ">");
		str = replace(str, "&lt;", "<");
		str = replace(str, "&#39;", "'");
		return str;
	}

	/**
	 * ��ԭ�����ַ� ���� & gt;  & lt;  ��
	 * @param obj a Object
	 * @return
	 */
	public String getRevertString(Object obj) {
		if (obj == null) {
			return "";
		}
		String str = obj.toString();
		str = replace(str, "&nbsp;", " ");
		str = replace(str, "<br>", "\n");
		str = replace(str, "&gt;", ">");
		str = replace(str, "&lt;", "<");
		str = replace(str, "&#39;", "'");

		return str;
	}

	/**
	 * ת���߼����������ַ� ����  '
	 * @param str a String
	 * @return
	 */
	public String getConvertHighString(String str) {
		if (str == null) {
			return "";
		}
		String strs = "";
		char[] t = str.toCharArray();
		for (int i = 0; i < t.length; i++) {
			switch (t[i]) {
			case 39:
				strs += "&#39;";
				break;
			default:
				strs += t[i];
			}
		}
		return strs;
	}

	/**
	 * ��ԭ�߼����������ַ� ����  &gt;  &lt;
	 * @param str
	 * @return
	 */
	public String getRevertHighString(String str) {
		if (str == null) {
			return "";
		}
		/*str = this.replace(str, "&gt;", ">");
		 str = this.replace(str, "&lt;", "<");
		 str=this.replace(str,"&quot;","\"");
		 str = this.replace(str, "&#39;", "'");
		 str = this.replace(str, "<br>", "\n");*/

		//str = this.replace(str, "&nbsp;", " ");
		//str = this.replace(str, "<br>", "\r");
		str = replace(str, ">", "&gt;");
		str = replace(str, "<", "&lt;");
		return str;
	}

	/**
	 * ��ȡclob�ֶΣ���ת�����ַ�
	 * @param lob a Clob
	 * @return String
	 */
	public String lobToTxt(Clob lob) {
		try {
			if (lob != null) {
				java.io.BufferedReader br = new java.io.BufferedReader(lob
						.getCharacterStream());
				String aa = "";
				String bb = "";
				while ((aa = br.readLine()) != null) {
					bb = bb + aa;
				}
				return (bb);
			} else {
				return "";
			}
		} catch (Exception e) {
			//com.bjtech.util.StringHandle.printLog("+++++++++++"+e.toString());
			return "";
		}
	}

	/**
	 * �ָ��ַ��һ������
	 * @param str ���ָ���ַ�
	 * @param dim Ҫ�ָ���ַ�
	 * @return
	 */
	public static String[] split(String str, String dim) {
		if (str == null || str.equals("")) {
			return null;
		}
		StringTokenizer st = new StringTokenizer(str, dim);
		int i = st.countTokens();
		String astr[] = new String[i];
		int k = 0;
		while (st.hasMoreTokens()) {
			astr[k] = st.nextToken();
			k++;
		}
		return astr;
	}

	public static String union(String[] str, String conjunction) {
		if (str == null || str.length < 1)
			return "";
		StringBuffer s = new StringBuffer();
		for (int i = 0; i < str.length; i++) {
			if (str[i] != null && str[i].length() > 0)
				s.append(str[i]).append(conjunction);
		}

		if (s.length() == 0)
			return "";

		return s.toString().substring(0, s.toString().lastIndexOf(conjunction));
	}

	/**
	 * Replaces all instances of oldString with newString in line.
	 * @param line the String to search to perform replacements on
	 * @param oldString the String that should be replaced by newString
	 * @param newString the String that will replace all instances of oldString
	 * @return a String will all instances of oldString replaced by newString
	 */
	public static final String replace(String line, String oldString,
			String newString) {
		if (line == null) {
			return null;
		}
		int i = 0;
		if ((i = line.indexOf(oldString, i)) >= 0) {
			char[] line2 = line.toCharArray();
			char[] newString2 = newString.toCharArray();
			int oLength = oldString.length();
			StringBuffer buf = new StringBuffer(line2.length);
			buf.append(line2, 0, i).append(newString2);
			i += oLength;
			int j = i;
			while ((i = line.indexOf(oldString, i)) > 0) {
				buf.append(line2, j, i - j).append(newString2);
				i += oLength;
				j = i;
			}
			buf.append(line2, j, line2.length - j);
			return buf.toString();
		}
		return line;
	}

	/**
	 * filter single quotes
	 * �ڲ�����ݿ�ʱ���˵����
	 * @param str a String
	 * @return
	 */
	public static String filter(String str) {
		if (str == null) {
			return "";
		}
		String temp = str;
		String reStr = "";
		int strlen = temp.length();
		for (int i = 0; i < strlen; i++) {
			if (temp.charAt(i) == 39) {
				reStr += "''";
			} else {
				reStr += temp.charAt(i);
			}
		}
		return reStr;
	}

	/**
	 * �ڲ�����ݿ�ʱת�����벢�ҹ��˵����
	 * @param str a String
	 * @return
	 */
	public String filterConvert(String str) {
		if (str == null) {
			return "";
		}
		String temp = toChinese(str);
		String reStr = "";
		int strlen = temp.length();
		for (int i = 0; i < strlen; i++) {
			if (temp.charAt(i) == 39) {
				reStr += "''";
			} else {
				reStr += temp.charAt(i);
			}
		}
		return reStr;
	}

	/**
	 * ת�������ı���
	 * @param strvalue a String
	 * @return ���ı����ַ�
	 */
	public static final String toChinese(String strvalue) {
		try {
			if (strvalue == null) {
				return "";
			} else {
				strvalue = new String(strvalue.getBytes("ISO8859_1"), "UTF-8");
				return strvalue;
			}
		} catch (Exception e) {
			return "";
		}
	}

	/**
	 * �ضϳ��ַ�
	 * @param str a String
	 * @param len ��������
	 * @return String
	 */
	public static String intercept(String str, int len) {
		if (str == null) {
			return "";
		}
		int t = str.length();
		len = len > 2 ? len : 2;
		if (t > len) {
			return str.substring(0, len - 2) + "...";
		} else {
			return str;
		}
	}

	/**
	 * ��ȡJS��ALERT��ʾ�������ص�ָ����URL
	 * @param alert
	 * @param url
	 * @return
	 */
	public String getJsAlert(String alert, String url) {
		String js = "";
		js = js + "<script language='javascript'>";
		js = js + "alert('" + alert + "');";
		if (url == null || url.equals(""))
			js = js + "history.back();";
		else
			js = js + "location.href='" + url + "'";
		js = js + "</script>";
		return js;
	}

	/**
	 * �ж��ַ��Ƿ�Ϊ��
	 * @param str �ַ�
	 * @return ���ΪNULL���򷵻ء���
	 */
	public String disposeNull(String str) {
		return str != null ? str : "";
	}

	/**
	 * �ж��ַ��Ƿ�Ϊ��
	 * @param str �ַ�
	 * @return ���ΪNULL���򷵻�ָ�����ַ�
	 */
	public String disposeNull(String str, String charStr) {
		return str != null ? str : charStr;
	}

	/**
	 * �ж��ַ��Ƿ�ΪNull�ַ�
	 * @param str �ַ�
	 * @return ���ΪNULL���򷵻�ָ�����ַ�
	 */
	public String disposeCNull(String str) {
		return str.equals("null") ? "" : str;
	}


	/**
	 * ����������ת��Ϊָ��С��λ��ʾ��FLOAT����
	 * @param obj ת���ĺ���
	 * @param sdigists С��λ����
	 * @return �����û�ָ��С��λ���ȵ�FLAOT����
	 */
	public static float toFloatmoney(Object obj, String sdigists) {
		float ftmp = 0;
		String stmp = "0";
		stmp = toStrmoney(obj, sdigists);
		ftmp = toFloat(stmp);
		return ftmp;
	}

	/**
	 * ���������Ͳ���ת����Ϊ�����ʾ
	 * @param obj ת���Ķ������
	 * @param sdigists ��ʾ��С��λ����
	 * @return �����û����õ�С��λ�����ʾ����
	 */
	public static String toStrmoney(Object obj, String sdigists) {
		java.text.DecimalFormat to = new java.text.DecimalFormat(sdigists);
		float ftmp = 0;
		String stmp = "0";
		try {
			ftmp = toFloat(obj);
			stmp = to.format(ftmp);
		} catch (Exception e) {
			printLog(e.getMessage());
			printLog("����toMoney�������");
		}
		return stmp;
	}

	/**
	 * ���ַ�ת����ΪFloat����
	 * @param sobj �ַ����ͺ���
	 * @return ���Ϊ���ַ�NULL������Ϊ0
	 */
	public static float toFloat(String sobj) {
		float ftmp = 0;
		String stmp = "";
		try {
			stmp = toStr(sobj, "0");
			ftmp = Float.parseFloat(stmp);
		} catch (Exception e) {
			printLog(e.getMessage());
			printLog("����toFloat�������");
		}
		return ftmp;
	}

	/**
	 * ����������ת��ΪFLOAT����
	 * @param obj �����������
	 * @return ����FLOAT���ͣ����Ϊ�գ�����Ϊ0
	 */
	public static float toFloat(Object obj) {
		float ftmp = 0;
		String stmp = "";
		try {
			stmp = toStr(obj, "0");
			ftmp = Float.parseFloat(stmp);

		} catch (Exception e) {
			printLog(e.getMessage());
			printLog("����toFloat�������");
		}
		return ftmp;
	}

	/**
	 */
	public static String toStr(Object obj) {
		return toStr(obj, "");
	}

	/**
	 * 
	 * @param obj ת���Ķ���
	 * @param replace �滻���ַ�
	 * @return   ���obj = null����ָ�����ַ�
	 */
	public static String toStr(Object obj, String replace) {
		return obj != null ? obj.toString() : replace;
	}

	/**
	 * @param obj
	 */
	public static int toInt(Object obj) {
		return toInt(obj, 0);
	}

	/**
	 * @param obj
	 */
	public static int toInt(Object obj, int replace) {
		return obj != null ? Integer.parseInt(obj.toString()) : replace;
	}

	/**
	 * @param date ʱ������
	 * @return
	 */
	public static String toDate(String date) {

		if (date != null || !date.equals("")) {
			date = " to_date('" + date + "', 'yyyy-mm-dd hh24:mi:ss') ";
		}
		return date;
	}

	/**
	 * @param date
	 * @return
	 */
	public static String toChar(String date) {
		if (date != null || !date.equals("")) {
			date = " to_char('" + date + "', 'yyyy-mm-dd hh24:mi:ss') ";
		}
		return date;
	}

	/**
	 * @param log 
	 */
	public static void printLog(Object log) {
		System.out.println(log);
	}

	/**
	 * @return
	 */
	public static String DatetoString() {
		Calendar cale = Calendar.getInstance();
		Date now = cale.getTime();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		//	 Date currentTime_1 = new Date();
		return formatter.format(now);
		//	return now;
	}
	
	public static  Date strToDate(String str) throws Exception {
	java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	if(str!=null&&!str.equals("")){
		return sdf.parse(str);
	}else{
		return null;
	}
	}
	
	

}